﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Flame.Dlr;

namespace Flame.Controls
{
    public partial class ScripterTabbedControl : UserControl
    {
        public class TextInputPropertyChangedEventArgs : EventArgs
        {
            public Panel InfoPanel { get; set; }
            public ATextInput TextInput { get; set; }
            public ScripterLoaderControl ScripterLoaderControl { get; set; }
        }
        public class NewScripterControlEventArgs : EventArgs
        {
            public ScripterLoaderControl ScripterLoaderControl { get; set; }
        }

        ScripterControlFormSettings _settings;
        TabControl _tabcontrol = new TabControl();
        ScopeParameters _scopeParameters;
        public event EventHandler<TextInputPropertyChangedEventArgs> TextInputPropertyChanged;
        public event EventHandler<NewScripterControlEventArgs> NewScripterControl;

        

        public ScripterTabbedControl( ScopeParameters scopeParameters, ScripterControlFormSettings settings)
        {
            InitializeComponent();
            _settings = settings;
            _scopeParameters = scopeParameters;
            Controls.Add(_tabcontrol);
            _tabcontrol.Dock = DockStyle.Fill;

            Controls.Add(_tabcontrol);

            //Load += ScripterTabbedControl_Load;
        }

        /*void ScripterTabbedControl_Load(object sender, EventArgs e)
        {
           // NewTab();
        }*/

        ScripterLoaderControl _newTab(ScripterLoaderControl tabpage)
        {
            if(NewScripterControl != null)
                NewScripterControl(this, new NewScripterControlEventArgs() { ScripterLoaderControl = tabpage });
            tabpage.TextInputPropertyChanged += (s, e) =>
            {
                if (TextInputPropertyChanged != null)
                    TextInputPropertyChanged(this, new TextInputPropertyChangedEventArgs() { InfoPanel = e.InfoPanel, TextInput = e.TextInput, ScripterLoaderControl = tabpage });
            };
            _tabcontrol.Controls.Add(tabpage);
            _tabcontrol.SelectedTab = tabpage;
             return tabpage;
        }
        ScripterLoaderControl _newTab()
        {
            return _newTab(new ScripterLoaderControl(new TextInputCreator(_settings.TextinputResolverType, new Manager(_scopeParameters, _settings.ManagerSettings)), _settings.DefaultLanguage));
        }

        public ScripterLoaderControl NewTab()
        {
            return _newTab();
        }
        public void CloseTab()
        {
           _tabcontrol.TabPages.Remove(_tabcontrol.SelectedTab);
        }
        /*public ScripterLoaderControl ImportTxtDialog()
        {
            var dialog = new OpenFileDialog();
            dialog.CheckFileExists = true;
            dialog.CheckPathExists = true;
            dialog.AddExtension = true;
            var result = dialog.ShowDialog();
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                var tabpage = new ScripterLoaderControl(_scopeParameters, _availableLanguages, _itextinputResolverType);
                tabpage.LoadFileTxt(dialog.FileName);
                _newTab(tabpage);
                return tabpage;
            }
            return null;
        }*/
        public ScripterLoaderControl LoadTab()
        {
            //if (CheckIsSaved() != SaveResult.Cancelled)
            {
                var dialog = new OpenFileDialog();
                dialog.CheckFileExists = true;
                dialog.CheckPathExists = true;
                dialog.AddExtension = true;
                var result = dialog.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    //bool alreadyOpen = false;
                    foreach (ScripterLoaderControl t in _tabcontrol.TabPages)
                    {
                        if (t.DocPath == dialog.FileName)
                        {
                            _tabcontrol.SelectedTab = t;
                            return t;
                            //alreadyOpen = true;
                        }
                    }
                  
                    var scripterLoaderControl = _newTab();
                    scripterLoaderControl.LoadFile(dialog.FileName);
                    return scripterLoaderControl;
                }
            }
            return null;
        }

        public ScripterLoaderControl ScripterLoaderControl
        {
            get
            {
                return (ScripterLoaderControl)_tabcontrol.SelectedTab;
            }
        }

        public void AskForClose(System.ComponentModel.CancelEventArgs e)
        {
            bool hasclosed;
            ScripterLoaderControl slc = ScripterLoaderControl;
            if (slc == null) return;
            do
            {
                 hasclosed = slc.AskForClose();

                 if (hasclosed)
                 {
                     _tabcontrol.Controls.Remove(slc);
                 }

                slc = ScripterLoaderControl;
            } while (hasclosed && slc != null);
             
            e.Cancel = !hasclosed;
        }
    }
}
